<?php

namespace App\Http\Controllers\Web;

use App\Http\Requests\LetterCreateRequest;
use App\Models\Letter;
use function GuzzleHttp\Psr7\str;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\Auth;

class LetterController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function __construct()
    {
        $this->middleware('auth')->except(['index','show']);
    }

    public function index()
    {
        $letters = Letter::where([['status','<>','1'],['scope',1]])->orderBy('created_at','desc')->get();
        foreach ($letters as $letter){
            $letter->created_time = $this->befordate($letter->created_at->getTimestamp());
            $letter->send_time = $this->afterdate($letter->send_time);
            $letter->to = $this->transto($letter->to);
            $letter->user = $this->transto($letter->user?$letter->user->email:"");
        }
        return view('letters.lists',['letters'=>$letters]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        return view('letters.create');
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(LetterCreateRequest $request)
    {
        $data = [
            'title'    => $request->get('title'),
            'content'  => $request->get('content'),
            'to'       => $request->get('to'),
            'send_time'=> $request->get('send_time'),
            'status'   => $request->get('status'),
            'scope'    => $request->get('scope'),
            'user_id'  => Auth::id(),
        ];
        $letter =  Letter::create($data);
        return redirect()->route('letters.show',[
           $letter->letter_id
        ]);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {

        $letter = Letter::find($id);
        if($letter->scope==2){
            if(Auth::id()!=$letter->user_id){
                flash()->error('这封信没有公开')->important();
                return redirect()->back();
            }
        }
        $letter->increment('views');
        $letter->user = $this->transto($letter->user->email);
        $letter->created_time = $this->befordate($letter->created_at->getTimestamp());
        $letter->send_time = $this->afterdate($letter->send_time);
        $letter->to = $this->transto($letter->to);
        return view('letters.show',['letter'=>$letter]);

    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {

    }


    public function befordate($the_time)
    {

        $now_time = time();
        $show_time = $the_time;
        $dur = $now_time - $show_time;
        if ($dur < 0) {
            return substr(date('Y-m-d H:i:s',$the_time),0,10);
        } else {
            if ($dur < 60) {
                return $dur . '秒前';
            } else {
                if ($dur < 3600) {
                    return floor($dur / 60) . '分钟前';
                } else {
                    if ($dur < 86400) {
                        return floor($dur / 3600) . '小时前';
                    } else {
                        if ($dur < 259200) {//3天内
                            return floor($dur / 86400) . '天前';
                        } else {
                            return substr(date('Y-m-d H:i:s',$the_time),0,10);
                        }
                    }
                }
            }
        }
    }

    function afterdate($the_time)
    {
        $now_time = time();
        $show_time = strtotime($the_time);
        $dur =$show_time-$now_time;
        if ($dur < 0) {
            return substr($the_time,0,10);
        } else {
            if ($dur < 60) {
                return $dur . '秒后';
            } else {
                if ($dur < 3600) {
                    return floor($dur / 60) . '分钟后';
                } else {
                    if ($dur < 86400) {
                        return floor($dur / 3600) . '小时后';
                    } else {
                        if ($dur < 259200) {//3天内
                            return floor($dur / 86400) . '天后';
                        } else {
                            return substr($the_time,0,10);
                        }
                    }
                }
            }
        }
    }

    public function transto ($str)
    {
        if(strpos($str, '@')){
            $email_array = explode("@", $str);
            $prevfix = (strlen($email_array[0]) < 4) ? "" : substr($str, 0, 3); //邮箱前缀
            $count = 0;
            $str = preg_replace('/([\d\w+_-]{0,100})@/', '***@', $str, -1, $count);
            $rs = $prevfix . $str;
            return $rs;
        }
        return "已消失";
    }
}
